package com.lr.falsework.base;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lr.falsework.common.bean.PageBean;
import com.ws.commons.interfac.Result;
import com.ws.mybatissmart.SmartMapper;



public class BaseController<S extends BaseService<E,M>,M  extends SmartMapper<E>,E  extends BaseEntity> {

	@Autowired
	protected S currentService;
	@Autowired
	protected M currentMapper;

	
	
	@ResponseBody
	@RequestMapping(path="/list",method=RequestMethod.POST)
	public Result list(@RequestBody E e,HttpServletResponse resp){
		e.startIfPageIng();
		List<E> list=currentService.selectByWhere(e); 
		return Result.success(PageBean.toMapIfPage(list)); 
	}
	@ResponseBody
	@RequestMapping(path="/add",method=RequestMethod.POST)
	public Result add(@RequestBody E e){
		return currentService.add(e);
	}
	
	@ResponseBody
	@RequestMapping(path="/update",method=RequestMethod.POST)
	public Result updateById(@RequestBody E e){
		return currentService.updateById(e);
	}
	
	
	@ResponseBody
	@RequestMapping(path="/details",method=RequestMethod.GET)
	public Result details(E  e,@RequestParam("id")Object idV){
		@SuppressWarnings("unchecked")
		Class<E> cl=(Class<E>)e.getClass();
		E data=currentService.selectById(idV,cl);
		return Result.success(data);
	}
	
	@ResponseBody
	@RequestMapping(path="/details-cond",method=RequestMethod.POST)
	public Result details(@RequestBody E e){
		E data=currentService.selectOneByWhere(e);
		return Result.success(data);
	}
	
	@ResponseBody
	@RequestMapping(path="/delete",method=RequestMethod.DELETE)
	public Result delete(E  e,@RequestParam("id")Object idV){
		@SuppressWarnings("unchecked")
		Class<E> cl=(Class<E>)e.getClass();
		return currentService.deleteById(idV,cl);
	}
	
	
}
